package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.MaintenancePlan;
import org.example.erp.service.impl.MaintenancePlanServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class tpmPlanController {
    @Autowired
    private MaintenancePlanServiceImpl service;


    @DeleteMapping("/tpmPlan/delete")
    public DataResult delete(@RequestBody List<String> planNo) {
        if (planNo.isEmpty()) {
            service.remove(null);
        } else {
            service.removeByIds(planNo);
        }
        return DataResult.success();
    }

    @GetMapping("/tpmPlan/find")
    public DataResult findReversoContexts(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                          @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize,
                                          @RequestParam(required = false) String planNo, @RequestParam(required = false) String planName,
                                          @RequestParam(required = false) String planType,
                                          @RequestParam(required = false) String status
    ) {
        LambdaQueryWrapper<MaintenancePlan> d = new LambdaQueryWrapper<>();
        IPage<MaintenancePlan> page = new Page<>(currentPage, pageSize);
        if (planNo != null && !planNo.isEmpty()) {
            d.like(MaintenancePlan::getPlanNo, planNo);
        }
        if (planName != null && !planName.isEmpty()) {
            d.like(MaintenancePlan::getPlanName, planName);
        }
        if (planType != null && !planType.isEmpty()) {
            d.like(MaintenancePlan::getPlanType, planType);
        }
        if (status != null && !status.isEmpty()) {
            d.like(MaintenancePlan::getStatus, status);
        }
        IPage<MaintenancePlan> pageResult = service.page(page, d);
        return DataResult.success(pageResult);
    }

    @PostMapping("/tpmPlan/insert")
    public DataResult insertReversoContexts(@RequestBody MaintenancePlan m) {
        service.save(m);
        return DataResult.success();
    }

    @PutMapping("/tpmPlan/update")
    public DataResult updateReversoContexts(@RequestBody MaintenancePlan m) {
        service.updateById(m);
        return DataResult.success();
    }
}

